$(function(){
	$("#signin-btn").on("click", function(e){
		e.stopPropagation();
		var account = $("#account").val().trim();
		var password = $("#password").val().trim();
		if(account === ''){
			alert("帐号不能为空")
		}else if(password === ''){
			alert("密码不能为空")
		}else{
			$.post('/api/auth/login/', {
				phone: account,
				password: password
			},function(res){
				if (res.code === 0) {
					window.location.href = 'index.html'
				}else{
					alert(res.msg)
				}
			}).error(function(res){
				var res = JSON.parse(res.responseText)
				alert(res.msg)
			})
		}
	})

	// 点击找回密码
	$("#find_password").on("click", function(e){
		e.stopPropagation();
		$("#passwordModal").modal("show");
	})

	// 获取验证吗
	var codeTimer = null;
	// 获取验证吗
	$("#get_vcode").on("click", function(e){
		e.stopPropagation();
		var that = $(this);
		var phone = $("#forget_phone").val().trim();
		if(phone === ""){
			alert("手机号码不能为空");
		}else if(!checkPhone(phone)){
			alert("手机号码格式不正确");
		}else{
			var option = {};
			option.phone = phone;
			option.operation = 1;
			option.success = function(res){
				var codeTime = 60;
				that.attr("disabled", true);
                codeTimer = setInterval(function () {
                  if (codeTime >= 0) {
                    that.text(codeTime);
                    --codeTime
                  } else {
                    clearInterval(codeTimer);
                    that.text("获取验证吗");
					that.removeAttr("disabled");
                  }
                }, 1000)
			}
			option.fail = function(res){
				alert("获取验证码失败，请重试");
			}
			getVerifyCode(option)
		}
	})
	//  确定找回密码
	$("#passwordModal .modal-sure").on("click", function(e){
		e.stopPropagation();
		var phone = $("#forget_phone").val().trim();
		var v_code = $("#v_code").val().trim();
		var new_password = $("#new_password").val().trim();
		var sure_new_password = $("#sure_new_password").val().trim();
		if(phone === ""){
			alert("手机号码不能为空");
		}else if(!checkPhone(phone)){
			alert("手机号码格式不正确");
		}else if(v_code === ""){
			alert("验证码不能为空");
		}else if(new_password === ""){
			alert("新密码不能为空");
		}else if(sure_new_password !== new_password){
			alert("前后两次密码输入不一致");
		}else{
			$.post("/api/auth/find_back/", {
				phone: phone,
				v_code: v_code,
				password: new_password
			}, function(res){
				if(res.code === 0){
					alert("修改密码成功");
					$("#passwordModal").find("input").val("");
					$("#passwordModal").modal("hide");
				}else{
					alert(res.msg)
				}
			}).error(function(res){
				var res = JSON.parse(res.responseText)
				alert(res.msg)
			})
		}
	})
})